﻿using EdgeSearch.Application.Manager.LogManager;
using EdgeSearch.Application.Manager.LogManager.DTOS;
using EdgeSearch.Core.Entity.LogDB;
using EdgeSearch.Core.Entity.Models;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EdgeSearch.Application.Services
{
    /// <summary>
    /// 日志服务
    /// </summary>
    public class LogServices : IScoped, IDynamicApiController
    {
        private readonly DatabaseLoggingManager _loggingManager;

        public LogServices(DatabaseLoggingManager loggingManager)
        {
            _loggingManager = loggingManager;
        }

        /// <summary>
        /// 获取缓冲区的日志列表
        /// </summary>
        /// <returns></returns>
        public List<LogInfo> GetBufferLogs()
        {
            return _loggingManager.GetBufferLogs();
        }

        /// <summary>
        /// 强制提交缓冲区的日志
        /// </summary>
        /// <returns></returns>
        public async Task<WebPageRespon<int>> SubmitBufferLog()
        {
            WebPageRespon<int> result = new WebPageRespon<int>();
            var cmd = await _loggingManager.SubmitBufferLogAsync();
            result.Data = cmd;
            result.Message = $"成功将缓冲区的{cmd}条日志持久化";
            return result;
        }


        /// <summary>
        /// 获取已储存的日志记录
        /// </summary>
        /// <param name="input">查询参数</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<WebPageRespon<List<LogInfo>>> GetLogList([FromQuery] SearchLogInputDto input)
        {
            return await _loggingManager.GetLogListAsync(input);
        }
    }
}
